package com.example.cpoopc.listview_test;

import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.ViewConfiguration;
import android.widget.LinearLayout;
import android.widget.Scroller;

public class MySrollLinearLayout extends LinearLayout{

	private Scroller mScrollA;
	private int mScaledTouchSlop;
	private int mMinFlingV;
	private int mMaxFlingV;
	private VelocityTracker mVTracker;
	private int mPointId;
	private float mDownX;
	private float mDownY;
	private float mLastMotionX;
	private float mLastMotionY;
		
	public MySrollLinearLayout(Context context) {
		super(context);
		init(context);
	}

	public MySrollLinearLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public MySrollLinearLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		init(context);
	}
	
	private void init(Context context) {
		ViewConfiguration localViewConfiguration = ViewConfiguration.get(context);
	    this.mScaledTouchSlop = localViewConfiguration.getScaledTouchSlop();
	    this.mMinFlingV = localViewConfiguration.getScaledMinimumFlingVelocity();
	    this.mMaxFlingV = localViewConfiguration.getScaledMaximumFlingVelocity();
		mScrollA = new Scroller(context);
		if (Build.VERSION.SDK_INT >= 11){
			setMotionEventSplittingEnabled(false);
		}
	}
	
	@Override
	public void computeScroll() {
		if(mScrollA.computeScrollOffset()){
			int currY = mScrollA.getCurrY();
			scrollTo(0, currY);
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event) {
		if(mVTracker == null){ 
			mVTracker = VelocityTracker.obtain();
		}
		mVTracker.addMovement(event);
		float currentX;
		float currentY;
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			currentX = event.getX();
			currentY = event.getY();
			mDownX = currentX;
			mDownY = currentY;
			mLastMotionX = currentX;
			mLastMotionY = currentY;
			mPointId = event.getPointerId(0);
//			mScrollA.forceFinished(true);
			mScrollA.startScroll(0, 0, 0, 600, 2000);
			break;
		case MotionEvent.ACTION_MOVE:
			int pointerIndex = event.findPointerIndex(mPointId);
			currentY = event.getY(pointerIndex);
			currentX = event.getX(pointerIndex);
			int shiftY = (int) Math.abs(currentY - mDownY);
			int shiftX = (int) Math.abs(currentX - mDownX);
			int deltaY = (int)(currentY - this.mLastMotionY);
			if(shiftY > mScaledTouchSlop && shiftY>shiftX){
				int toY = getScrollY() - deltaY;
				scrollTo(0, toY);
				invalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			VelocityTracker vTracker = mVTracker;
			vTracker.computeCurrentVelocity(1000, mMaxFlingV);
			if(mVTracker != null){
				mVTracker.recycle();
				mVTracker = null;
			}
			break;

		default:
			break;
		}
		return super.dispatchTouchEvent(event);
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return true;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	}
	
	@Override
	public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) {
		super.requestDisallowInterceptTouchEvent(disallowIntercept);
	}
}
